package com.team2.outpatient_sys.dao;

import com.team2.outpatient_sys.pojo.*;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Result;

import java.util.List;

@Mapper
public interface OutpatientScheduleDao {
    @Select("select * from schedule where id=#{id}")
    public Schedule findById(int id);

    /**
     * 查询排班表与医生关系表，按星期和班期升序排序，找出所有排班及对应医生
     * @return 所有排班的list
     */
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "week",column = "week"),
            @Result(property = "shift",column = "shift"),
            @Result(property = "deptId",column = "dept_id"),
            @Result(property = "doctors",column = "doctor_id",many = @Many (select = "com.team2.outpatient_sys.dao.DoctorDao.queryById"))
    })
    @Select("select s.*,d_s.doctor_id from schedule s,doctor_schedule d_s where s.id = d_s.schedule_id order by dept_id,week,shift")
    List<ScheduleAndDoctor> findAllSchedule();

    /**
     * 查询排班表、医生表与医生排班关系表，按星期和班期升序排序，根据医生姓名找出相应排班
     * @param name 医生姓名
     * @return 相应排班的list
     */
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "week",column = "week"),
            @Result(property = "shift",column = "shift"),
            @Result(property = "deptId",column = "dept_id"),
            @Result(property = "doctors",column = "doctor_id",many = @Many (select = "com.team2.outpatient_sys.dao.DoctorDao.queryById"))
    })
    @Select("select s.*,d_s.doctor_id from schedule s,doctor d,doctor_schedule d_s where d.name like #{name} and d.id = d_s.doctor_id and s.id = d_s.schedule_id order by dept_id,week,shift")
    List<ScheduleAndDoctor> findScheduleByDoctorName(String name);

    /**
     * 修改医生排班关系表中的医生id
     * @param dId 医生id
     * @param sId 排班id
     * @return 是否修改成功
     */
    @Update("update doctor_schedule set doctor_id = #{dId} where schedule_id = #{sId}")
    int updateDoctorSchedule(Integer dId,Integer sId);

    /**
     * 以医生id分组，查询医生表与医生排班关系表，根据已排班次数升序排序返回所有医生
     * @return 排序后的所有医生对象的list
     */
    @Select("select d.*,count(d_s.doctor_id) num from (select * from doctor) d left join (select doctor_id from doctor_schedule) d_s on d.id = d_s.doctor_id group by d.id order by num")
    List<DoctorAndNum> findAllDoctorAsc();

    /**
     * 查询科室表，将name映射为text，id映射为value，返回所有科室筛选器
     * @return 所有科室筛选器的list
     */
    @Results({
            @Result(property = "text",column = "name"),
            @Result(id = true,property = "value",column = "id")
    })
    @Select("select * from department where id > 2")
    List<DeptFilter> findAllDeptFilter();

    /**
     * 根据排班id查询医生排班关系表，返回预约名额
     * @param sId 排班id
     * @return 对应预约名额
     */
    @Results({
            @Result(property = "schedule",column = "schedule_id",one = @One(select = "com.team2.outpatient_sys.dao.OutpatientScheduleDao.findById")),
            @Result(property = "appointmentNo",column = "appointment_no"),
    })
    @Select("select schedule_id,appointment_no from doctor_schedule where schedule_id = #{sId}")
    List<Appointment> findAppointmentBySId(Integer sId);

    /**
     * 查询排班表、医生表，医生排班关系表，根据科室id与星期返回值班医生和预约名额
     * @param deptId 科室id
     * @param week 星期几
     * @return 对应值班医生和预约名额
     */
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "name",column = "name"),
            @Result(property = "username",column = "username"),
            @Result(property = "password",column = "password"),
            @Result(property = "gender",column = "gender"),
            @Result(property = "birth",column = "birth"),
            @Result(property = "phone",column = "phone"),
            @Result(property = "deptId",column = "dept_id"),
            @Result(property = "hiredate",column = "hiredate"),
            @Result(property = "isDirector",column = "is_director"),
            @Result(property = "appointment",column = "schedule_id",many = @Many (select = "com.team2.outpatient_sys.dao.OutpatientScheduleDao.findAppointmentBySId")),
    })
    @Select("select d.*,s.id schedule_id from doctor d,schedule s,doctor_schedule d_s where s.dept_id = #{deptId} and s.week = #{week} and d.id = d_s.doctor_id and s.id = d_s.schedule_id")
    List<DoctorAndAppointment> findDoctorAndAppointment(Integer deptId,Integer week);
}
